<?php

if(!isset($_SESSION)) {
    //对于mobile端，采用其发过来的session_id
    if(isset($_REQUEST["session_id"])) {
        session_id($_REQUEST["session_id"]);
    }
    session_start();
}

include_once( './data/weibo.config.php' );
include_once( './api/saetv2.ex.class.php' );
include_once( './data/defines.php' );
include_once( './api/api_suggest.php' );
include_once( './api/api_like.php' );

//判断用户是否授权
if (empty($_SESSION[SESS_ACCESS_TOKEN])) {
    //若没有获取到access token，则返回错误
    $arr = array(
        FLAG_RET => ERROR,
        FLAG_ERR_CODE => ERR_AUTH
    );
    echo json_encode($arr);
    exit;
} else {
    //若已获取到access token，则加载应用信息
    $c = new SaeTClientV2( WB_AKEY , WB_SKEY ,$_SESSION[SESS_ACCESS_TOKEN] ,'' );	
}

$ret = array(
    FLAG_RET => ERROR,
    FLAG_ERR_CODE => ERR_UNKNOWN
);

switch($_REQUEST['action']) {

    case 'get_list': {
        $uid_get = $c->get_uid();
        $uid = $uid_get['uid'];
        $ret = $c->place_user_timeline_by_id($uid, 1 , 50 , 0, 0, 0);
        $ret[FLAG_RET] = NO_ERR;
        break;
    }
    
    case 'weibo_get': {
        $id_list = $_REQUEST['id_list'];
        $success = false;
        if(isset($id_list)) {
            $statuses = array();
            $i = 0;
            foreach ($id_list as $weibo_id) {
                $var = $c->show_status($weibo_id);
                $statuses[$i] = $var;
                $i++;
            }
            $ret = array(
                FLAG_RET => NO_ERR,
                FLAG_STATUSES => $statuses
            );
            $success = true;
        }
        
        if(!$success) {
            $ret = array(
                FLAG_RET => ERROR,
                FLAG_ERR_CODE => ERR_VAL
            );
        }
        break;
    }
    
    case 'get_comment': {
        $geoweibo_mid = $_REQUEST['geoweibo_mid'];
        if(isset($geoweibo_mid)) {
            $ret = $c->get_comments_by_sid($geoweibo_mid);
            $ret[FLAG_RET] = NO_ERR;
        } else {
            $ret = array(
                FLAG_RET => ERROR,
                FLAG_ERR_CODE => ERR_VAL
            );
        }
        break;
    }
    
    case 'send_comment': {
        $comment_text = $_REQUEST['comment_text'];
        $geoweibo_mid = $_REQUEST['geoweibo_mid'];
        if(isset($geoweibo_mid) && isset($comment_text)) {
            $ret = $c->send_comment($geoweibo_mid, $comment_text);
            $ret[FLAG_RET] = NO_ERR;
        } else {
            $ret = array(
                FLAG_RET => ERROR,
                FLAG_ERR_CODE => ERR_VAL
            );
        }
        break;
    }
    
    case 'suggest_add': {
        $geoweibo_id = $_REQUEST['geoweibo_id'];
        $geoweibo_mid = $_REQUEST['geoweibo_mid'];
        $latitude = $_REQUEST['latitude'];
        $longtitude = $_REQUEST['longtitude'];
        $weibo_text = $_REQUEST['weibo_text'];
        $weibo_nickname = $_REQUEST['weibo_nickname'];
        $ret = suggest_add($geoweibo_id, $geoweibo_mid, $latitude, $longtitude, $_SESSION[SESS_USER_ID], $weibo_text, $weibo_nickname);
        
        break;
    }
    
    case 'suggest_get': {
        $ret = suggest_get($_SESSION[SESS_USER_ID]);
        break;
    }
    
    case 'like_act': {
        $act = $_REQUEST['act'];
        $suggest_weibo_id = $_REQUEST['suggest_weibo_id'];
        
        if($act == "add") {
            $ret = like_add($suggest_weibo_id, $_SESSION[SESS_USER_ID]);
        } else if($act == "del"){
            $ret = like_del($suggest_weibo_id, $_SESSION[SESS_USER_ID]);
        } else {
            $ret = array(
                FLAG_RET => ERROR,
                FLAG_ERR_CODE => ERR_VAL
            );
        }
        break;
    }
    
    default: {
        $ret = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_VAL
        );
        break;
    }
}

echo json_encode($ret);
?>